import React from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import './App.css';

// 页面组件
import LoginPage from './pages/LoginPage';
import RegisterPage from './pages/RegisterPage';
import DashboardPage from './pages/DashboardPage';
import IssueListPage from './pages/IssueListPage';
import IssueDetailPage from './pages/IssueDetailPage';
import IssueCreatePage from './pages/IssueCreatePage';
import IssueEditPage from './pages/IssueEditPage';
import CaseLibraryPage from './pages/CaseLibraryPage';
import CaseDetailPage from './pages/CaseDetailPage';
import CaseCreatePage from './pages/CaseCreatePage';
import CaseEditPage from './pages/CaseEditPage';
import ResourceCenterPage from './pages/ResourceCenterPage';
import ProfilePage from './pages/ProfilePage';
import SettingsPage from './pages/SettingsPage';
import ReportsPage from './pages/ReportsPage';

// 布局组件
import MainLayout from './components/MainLayout';
import ErrorBoundary from './components/ErrorBoundary';
import { UserProvider } from './contexts/UserContext';

// 简单的认证检查函数
const isAuthenticated = () => {
  return !!localStorage.getItem('access_token');
};

// 受保护的路由组件
const ProtectedRoute = ({ children }) => {
  const isAuth = isAuthenticated();
  console.log('ProtectedRoute检查:', {
    hasToken: !!localStorage.getItem('access_token'),
    isAuthenticated: isAuth,
    willRender: isAuth ? 'children' : 'redirect to login'
  });
  
  if (!isAuth) {
    console.log('用户未认证，重定向到登录页');
    return <Redirect to="/login" />;
  }
  
  console.log('用户已认证，渲染子组件');
  return children;
};

function App() {
  return (
    <UserProvider>
      <ErrorBoundary>
        <Router>
        <Switch>
          <Route path="/login" component={LoginPage} />
          <Route path="/register" component={RegisterPage} />
          <Route exact path="/" render={() => (
            <ProtectedRoute>
              <ErrorBoundary>
                <MainLayout>
                  <DashboardPage />
                </MainLayout>
              </ErrorBoundary>
            </ProtectedRoute>
          )} />
        <Route exact path="/issues" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <IssueListPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route exact path="/issues/:id" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <IssueDetailPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/issues/:id/edit" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <IssueEditPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/create-issue" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <IssueCreatePage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route exact path="/cases" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <CaseLibraryPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/cases/create" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <CaseCreatePage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/cases/:id/edit" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <CaseEditPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/cases/:id" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <CaseDetailPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/resources" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <ResourceCenterPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/profile" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <ProfilePage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/settings" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <SettingsPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
        <Route path="/reports" render={() => (
          <ProtectedRoute>
            <ErrorBoundary>
              <MainLayout>
                <ReportsPage />
              </MainLayout>
            </ErrorBoundary>
          </ProtectedRoute>
        )} />
      </Switch>
    </Router>
  </ErrorBoundary>
    </UserProvider>
  );
}

export default App;